package javaMSCRM;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import com.microsoft.schemas.crm._2006.query.ConditionOperator;
import com.microsoft.schemas.crm._2006.query.PagingInfo;
import com.microsoft.schemas.crm._2006.webservices.BusinessEntity;
import com.microsoft.schemas.crm._2006.webservices.BusinessEntityCollection;
import com.microsoft.schemas.crm._2007.webservices.Lead;
import com.microsoft.schemas.crm._2007.webservices.EntityName;
/**
 * @author Zoran IVANOV
 *
 */
public class Leads extends Entities {
	/**
	 * @param JavaMSCRM
	 */
	public Leads(JavaMSCRM JavaMSCRM) {
		super(JavaMSCRM);
		this.Entity = EntityName.LEAD.toString();
		JavaMSCRM.Entity = this.Entity;
		this.ViewTitle = "Leads";	
		setColumnEntitiesNames(new String[]{"subject", "fullname", "statuscode", "createdon", "companyname", "address1_country"});
		setColumnEntitiesTitles(new String[]{"Topic", "Name", "Status Reason", "Created On", "Company", "Country/Region"});
		setViewFilterLabels(new String[]{"My Open Leads", "Open Leads"});
		JavaMSCRM.HeaderTitle.setText(this.ViewTitle);
		
		//Initialize Filter ComboBox
		initialiseViewComboBox(this, JavaMSCRM.ViewFilterJCB, JavaMSCRM.SearchField);
		
		//Initialize Search
		initialiseSearchForm(this, JavaMSCRM.ViewFilterJCB);
		
		displayEntitiesList(pageInfo, 0, 1, "");
	}

	/* (non-Javadoc)
	 * @see JavaMSCRMEntities#displayEntitiesList(com.microsoft.schemas.crm._2006.query.PagingInfo, int, int, java.lang.String)
	 */
	public void displayEntitiesList(PagingInfo pageInfo, int sortingColumn, int sortingDirection, String letterFilter){
	 	this.SortingColumn = sortingColumn;
	 	this.SortingDirection = sortingDirection;
	 	this.letterFilter = letterFilter;
	 	JavaMSCRM.EntriesIDList = new ArrayList<String>();
	 	setMSCRMConditions(new Condition[]{
				new Condition(!searchString.equals(""), "subject", ConditionOperator.LIKE, "%"+searchString+"%"),
				new Condition(!letterFilter.equals(""), "subject", ConditionOperator.LIKE, letterFilter+"%"),
				new Condition((viewFilter == true) && (searchString.equals("")), "ownerid", ConditionOperator.EQUAL, JavaMSCRM.UserId),
				new Condition(true, "statecode", ConditionOperator.EQUAL, "Open")
				});
	 	
		try {	
            //Table Headers
            displayTableHeader(this, letterFilter);
	        
		    JavaMSCRM.add(JavaMSCRM.TableScrollPane, BorderLayout.CENTER);
		    
		    JavaMSCRM.table.addMouseListener(new MouseAdapter(){
		        public void mouseClicked(MouseEvent e){
		        	int row = JavaMSCRM.table.getSelectedRow();
		        	if (e.getClickCount() == 2){
		        		new JavaMSCRMLead(JavaMSCRM, JavaMSCRM.EntriesIDList.get(row));
		            }
		         }
		        } );
		    		         
		    //Retrieve Records
            BusinessEntityCollection bec = JavaMSCRM.stub.RetrieveMultiple(getListOfEntities(pageInfo),  JavaMSCRM.catd, null, null).getRetrieveMultipleResponse().getRetrieveMultipleResult(); 
            BusinessEntity[] Records = bec.getBusinessEntities().getBusinessEntityArray(); 
            
            //Display Records
            if(Records.length > 0){
            	JavaMSCRM.MainMessage.setText("");
	            for(int i=0; i<Records.length; i++){   
	            	Lead Record = (Lead) Records[i];           		            		
	                JavaMSCRM.model.addRow(new Object[]{Record.getSubject(), Record.getFullname(), Record.getStatuscode().getName(), Record.getCreatedon().getDate(), Record.getCompanyname(), Record.getAddress1Country() });
	                JavaMSCRM.EntriesIDList.add(Record.getLeadid().getStringValue());
	            }
            }else{
            	JavaMSCRM.MainMessage.setText("No records available in this view!");
            }
            
            //Initialize Letter Filter and Page Navigation
            initialiseLetterFilterAndNavigation(bec);
            
	        JavaMSCRM.table.setVisible(true);
		}  
		catch (Exception e) {
			JOptionPane.showMessageDialog(null, e.toString());
		}    	
	}
}
